/*
 * @(#)Duration.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Map;
import java.util.TreeMap;

/**
 * Duration.java
 *
 * Representation of a time span.
 *
 *   @author            Lun Au
 *   @company       DCIVision Limited
 *   @creation date  26/09/2006
 *   @version          $Revision: 1.2 $
 */

public class Duration {

    private long startMillis;
    private long endMillis;
    private long diffMillis;
    private Map durations = new TreeMap();

    /**
     * @param startMillis
     * @param endMillis
     */
    public Duration ( long startMillis, long endMillis ) {
        this.startMillis = startMillis;
        this.endMillis = endMillis;
        calculate();
    }

    /**
     * @param startMillis
     */
    public void setStartMillis ( long startMillis ) {
        this.startMillis = startMillis;
        calculate();
    }

    /**
     * @param endMillis
     */
    public void setEndMillis ( long endMillis ) {
        this.endMillis = endMillis;
        calculate();
    }

    private void calculate() {
        this.diffMillis = endMillis-startMillis;
        Calendar calStart = GregorianCalendar.getInstance();
        Calendar calEnd = GregorianCalendar.getInstance();

        calStart.setTimeInMillis ( startMillis );
        calEnd.setTimeInMillis ( endMillis );

        durations.put ( String.valueOf(Calendar.YEAR), new Long(DateUtility.abs_diff (Calendar.YEAR, calStart,calEnd)) );
        durations.put ( String.valueOf(Calendar.MONTH), new Long(DateUtility.abs_diff (Calendar.MONTH, calStart,calEnd)) );
        durations.put ( String.valueOf(Calendar.DAY_OF_MONTH), new Long(DateUtility.abs_diff (Calendar.DAY_OF_MONTH, calStart,calEnd)) );
        durations.put ( String.valueOf(Calendar.HOUR_OF_DAY), new Long(DateUtility.abs_diff (Calendar.HOUR_OF_DAY, calStart,calEnd)) );
        durations.put ( String.valueOf(Calendar.MINUTE), new Long(DateUtility.abs_diff (Calendar.MINUTE, calStart,calEnd)) );
        durations.put ( String.valueOf(Calendar.SECOND), new Long(DateUtility.abs_diff (Calendar.SECOND, calStart,calEnd)) );
        durations.put ( String.valueOf(Calendar.MILLISECOND), new Long(DateUtility.abs_diff (Calendar.MILLISECOND, calStart,calEnd)) );

    }


    /**
     * Gets the duration of total milliseconds before format
     * @return
     */
    public long getMillisDiff() {
        return diffMillis;
    }

    /**
     * Gets the duration of a specific field after divided into X days Y hours format
     * @param field
     * @return The duration
     */
    public long get( int field ) {
        if ( durations.containsKey ( String.valueOf(field) ) ) {
            return ((Long)durations.get (String.valueOf(field))).longValue();
        }
        return 0;
    }
}